package fr.sedoo.commons.client.faq.ui.list;

import com.google.gwt.cell.client.Cell.Context;
import com.google.gwt.safehtml.shared.SafeHtmlBuilder;
import com.google.gwt.user.client.ui.Image;

import fr.sedoo.commons.client.image.CommonBundle;
import fr.sedoo.commons.client.util.UuidUtil;
import fr.sedoo.commons.shared.domain.faq.QuestionAndAnswer;


public class FaqRenderer
{
	private final static String QUESTION_PREFIX= "QUESTION_";
	private final static String IMG_PREFIX= "IMG_";
	private final static String ANSWER_PREFIX= "ANSWER_";

	public static void render(Context context, QuestionAndAnswer value, SafeHtmlBuilder sb)
	{
		if (value == null)
		{
			return;
		}

		String uuid = UuidUtil.uuid();
		String questionId=QUESTION_PREFIX+uuid;
		String imageId=IMG_PREFIX+uuid;
		String answerId=ANSWER_PREFIX+uuid;
		Image dot = new Image(CommonBundle.INSTANCE.menuDot());
		dot.getElement().setId(imageId);
		dot.getElement().getStyle().setProperty("cursor", "pointer");
		sb.appendHtmlConstant("<table width=\"100%\" style=\"border-bottom: 1px solid #EEEEEE;padding:5px;text-align:justify;cursor:default;border-collapse:collapse\" class=\"localCellList\">");
		sb.appendHtmlConstant("<tr><td colspan=\"2\">"+dot.toString()+"<span style=\"margin-left:5px;cursor:pointer;font-weight:bold\" id=\""+questionId+"\">"+value.getQuestion()+"</span></td></tr>");
		sb.appendHtmlConstant("<tr id=\""+answerId+"\" style=\"display:none\"><td>&nbsp;</td><td style=\"text-align: justify;\">");
		sb.appendHtmlConstant(value.getAnswer());		
		sb.appendHtmlConstant("</td></tr>");
		sb.appendHtmlConstant("</table>");
		sb.appendHtmlConstant("<div style=\"height:4px\">");
	}
	
	public static final String getAnwerIdFromSrcId(String questionId)
	{
		String[] split = questionId.split("_");
		return ANSWER_PREFIX+split[1];
	}

	public static boolean isSrcId(String id) {
		if ((id.startsWith(IMG_PREFIX)) || (id.startsWith(QUESTION_PREFIX)))
		{
			return true;
		}
		else
		{
			return false;
		}
	}

}
